package chapter02

/**
 * @program lu-scala01
 * @description: 函数练习
 * @author: zhanglu
 * @create: 2022-02-15 21:28:00
 */
object C08_FuncPractice {

  def main(args: Array[String]): Unit = {
    test03
  }

  def test01() {
    def f(a: String, b: Char, c: Int) = if (a == "a" && b == 'b' && c == 0) true else false

    println(f("a", 'b', 0))
    println(f("a", 'b', 1))

    //闭包
    def f1(a: String): Char => (Int => Boolean) = {
      def f2(b: Char): Int => Boolean = {
        def f3(c: Int): Boolean = {
          if (a == "a" && b == 'b' && c == 0) true else false
        }

        f3
      }

      f2
    }

    println(f1("a")('b')(0))
    println(f1("a")('b')(1))

    var ff1 = (a: String) => {
      (b: Char) => {
        (c: Int) => {
          if (a == "a" && b == 'b' && c == 0) true else false
        }
      }
    }
    println(ff1("a")('b')(0))
    println(ff1("a")('b')(1))

    def ff2(a: String): Char => (Int => Boolean) = b => c => {
      if (a == "a" && b == 'b' && c == 0) true else false
    }

    println(ff2("a")('b')(0))
    println(ff2("a")('b')(1))

    //柯里化
    def fff1(a: String)(b: Char)(c: Int): Boolean = {
      if (a == "a" && b == 'b' && c == 0) true else false
    }

    println(fff1("a")('b')(0))
    println(fff1("a")('b')(1))

  }

  /**
   * 函数闭包
   */
  def test02() {

    def add(a: Int, b: Int): Int = {
      a + b
    }

    //将固定的加数作为另一个参数传入
    def add2(a: Int): Int => Int = {
      def f(b: Int): Int = {
        a + b
      }

      f
    }

    println(add2(1)(2))

    def add3(a: Int): Int => Int = {
      (b: Int) => {
        a + b
      }
    }

    println(add3(1)(2))

    //lambda
    val add4 = (a: Int) => (b: Int) => a + b
    println(add4(1)(2))

    def add5(a: Int): Int => Int = a + _

    println(add5(1)(2))

  }

  /**
   * 函数柯里化
   */
  def test03() {
    //基于 test2
    def f1(a: Int)(b: Int): Int = {
      a + b
    }

    println(f1(1)(2))

    def f2(a: Int)(b: Int) = a + b

    println(f2(1)(2))
  }

}
